$(function(){

	$("#foodcollocation-add-edit select.foodsectionid").change(function(){
		var sid = $("#foodcollocation-add-edit select.foodsectionid :selected").val();
		$.post(APP+'/FoodCollocation/returncategorys', {id: sid}, function(data){
			var obj = JSON.parse(data);
			var foodcategory = $("#foodcollocation-add-edit select.foodcategoryid");
			foodcategory.html("");
			$('<option value="0">---请选择所属分类---</option>').appendTo(foodcategory);
			for(var i=0; i<obj.length; i++){
				$('<option value="'+obj[i].id+'">'+obj[i].name+'</option>').appendTo(foodcategory);
			}
		});
	});

	$("#foodcollocation-add-edit select.foodcategoryid").change(function(){
		var sid = $("#foodcollocation-add-edit select.foodsectionid :selected").val();
		var cid = $("#foodcollocation-add-edit select.foodcategoryid :selected").val();
		$.post(APP+'/FoodCollocation/returnarticles', {sid: sid,cid: cid}, function(data){
			var obj = JSON.parse(data);
			var foodarticle = $("#foodcollocation-add-edit select.foodarticleid");
			foodarticle.html("");
			$('<option value="0">---请选择食材名称---</option>').appendTo(foodarticle);
			for(var i=0; i<obj.length; i++){
				$('<option value="'+obj[i].id+'">'+obj[i].name+'</option>').appendTo(foodarticle);
			}
		});
	});

	$("#foodcollocation-add-edit select.foodarticleid").change(function(){

		var sid = $("#foodcollocation-add-edit select.foodsectionid :selected").val();
		var cid = $("#foodcollocation-add-edit select.foodcategoryid :selected").val();
		var id = $("#foodcollocation-add-edit select.foodarticleid :selected").val();

		var orgimg = $("#foodcollocation-add-edit .orgimg");

		$.post(APP+'/FoodCollocation/returnarticles', {sid: sid,cid: cid,id: id}, function(data){
			var obj = JSON.parse(data);
			var destarticle = $("#foodcollocation-add-edit .orgimg");
			orgimg.attr('src',PUBLIC+'/Content/FoodArticle/thumb_'+obj.image);
		});

	});

	$("#foodcollocation-add-edit .adds").click(function(){
		var section = $("#foodcollocation-add-edit [name='destsectionid[]']");
		section.removeClass();
		var category = $("#foodcollocation-add-edit [name='destcategoryid[]']");
		category.removeClass();
		var article = $("#foodcollocation-add-edit [name='destarticleid[]']");
		article.removeClass();
		var destimg = $("#foodcollocation-add-edit [alt='destimg']");
		destimg.removeClass();

		var content = $("#foodcollocation-add-edit .content");
		var dest = $("#foodcollocation-add-edit .adddest");

		var html = dest.html();

		$(html).appendTo(content);

	});

	$("#foodcollocation-add-edit .remove").live("click",function(){
		var num = $("#foodcollocation-add-edit .remove").size();
		var abc = $(this).parents('.dest');
		if(num>2){
			abc.remove();
		}else{
			alert('只有一个不能删除');
		}
	});

	$("#foodcollocation-add-edit .dest").live("click",function () {

		var section = $("#foodcollocation-add-edit [name='destsectionid[]']");
		section.removeClass();
		var category = $("#foodcollocation-add-edit [name='destcategoryid[]']");
		category.removeClass();
		var article = $("#foodcollocation-add-edit [name='destarticleid[]']");
		article.removeClass();
		var destimg = $("#foodcollocation-add-edit [alt='destimg']");
		destimg.removeClass();

		var content = $(this);

		var destsection = $(this).find("[name='destsectionid[]']");
		destsection.addClass('destsection');

		var destcategory = $(this).find("[name='destcategoryid[]']");
		destcategory.addClass('destcategory');

		var destarticle = $(this).find("[name='destarticleid[]']");
		destarticle.addClass('destarticle');

		var destimgs = $(this).find("[alt='destimg']");
		destimgs.addClass('destimg');

	});

	$("#foodcollocation-add-edit select.destsection").live("change",function(){

			var sid = $("#foodcollocation-add-edit select.destsection :selected").val();
			$.post(APP+'/FoodCollocation/returncategorys', {id: sid}, function(data){
				 var obj = JSON.parse(data);
				 var destcategory = $("#foodcollocation-add-edit select.destcategory");
				 destcategory.html("");
				 $('<option value="0">---请选择所属分类---</option>').appendTo(destcategory);
				 for(var i=0; i<obj.length; i++){
				 	$('<option value="'+obj[i].id+'">'+obj[i].name+'</option>').appendTo(destcategory);
				 }
			 });

	});

	$("#foodcollocation-add-edit select.destcategory").live("change",function(){

		var sid = $("#foodcollocation-add-edit select.destsection :selected").val();
		var cid = $("#foodcollocation-add-edit select.destcategory :selected").val();
		var oaid = $("#foodcollocation-add-edit select.foodarticleid :selected").val();

		$.post(APP+'/FoodCollocation/returnarticles', {sid: sid,cid: cid,oaid:oaid}, function(data){
			var obj = JSON.parse(data);
			var destarticle = $("#foodcollocation-add-edit select.destarticle");
			destarticle.html("");
			$('<option value="0">---请选择食材名称---</option>').appendTo(destarticle);
			for(var i=0; i<obj.length; i++){
				$('<option value="'+obj[i].id+'">'+obj[i].name+'</option>').appendTo(destarticle);

			}
		});
	});

	$("#foodcollocation-add-edit select.destarticle").live("change",function(){

		var sid = $("#foodcollocation-add-edit select.destsection :selected").val();
		var cid = $("#foodcollocation-add-edit select.destcategory :selected").val();
		var id = $("#foodcollocation-add-edit select.destarticle :selected").val();

		var destimg = $("#foodcollocation-add-edit .destimg");

		$.post(APP+'/FoodCollocation/returnarticles', {sid: sid,cid: cid,id: id}, function(data){
			var obj = JSON.parse(data);

			destimg.attr('src',PUBLIC+'/Content/FoodArticle/thumb_'+obj.image);
		});

	});

	$("#foodcollocation-index  .nav a").each(function(index){
		var a = $(this);
		if(a.attr('href')==SELF){
			a.css('background-color','yellow');
			return false;
		}
		if(eval("/"+sids+"/.test(a.attr('href'))")){
			a.css('background-color','yellow');
			return false;
		}
	});

	$("#foodcollocation-index  .cnav a").each(function(index){
		var a = $(this);
		if(a.attr('href')==SELF){
			a.css('background-color','yellow');
			return false;
		}
		if(eval("/"+cids+"/.test(a.attr('href'))")){
			a.css('background-color','yellow');
			return false;
		}
	});

/*
	$("#foodcollocation-add-edit table textarea").each(function(index){
		var li = $(this);
		li.click(function(){
			$("#foodcollocation-add-edit table textarea").removeClass("textarea");
			li.addClass("textarea");
		});
	});
*/

	$("#foodcollocation-add-edit input.submit").click(function(){
		var sid = $("#foodcollocation-add-edit select.foodsectionid :selected").val();
		var cid = $("#foodcollocation-add-edit select.foodcategoryid :selected").val();
		var aid = $("#foodcollocation-add-edit select.foodarticleid :selected").val();

		if(sid==0){
			alert('请选择所属栏目');
			return false;
		}else if(cid==0){
			alert('请选择所属分类');
			return false;
		}else if(aid==0){
			alert('请选择食材');
			return false;
		}

		var dsid = $("#foodcollocation-add-edit select.destsectionid :selected").val();
		var dcid = $("#foodcollocation-add-edit select.destcategoryid :selected").val();
		var daid = $("#foodcollocation-add-edit select.destarticleid :selected").val();
		var caid = $("#foodcollocation-add-edit select.collocaltion :selected").val();

		if(dsid==0){
			alert('请选择所属栏目');
			return false;
		}else if(dcid==0){
			alert('请选择所属分类');
			return false;
		}else if(daid==0){
			alert('请选择食材');
			return false;
		}

	});

	$("#foodcollocation-index .allselect").toggle(function(){
		$("#foodcollocation-index input[type=checkbox]").attr('checked', 'checked');
	},function(){
		$("#foodcollocation-index input[type=checkbox]").attr('checked',null);
	});

	$("#foodcollocation-index .buttondel").click(function(){
		var datas = $("#foodcollocation-index form.delete").serializeArray();
		if(datas==''){
			alert('请选择要删除的食材搭配');
		}
		else{
			if(window.confirm('确认删除这些食材搭配?')){
				$("#foodcollocation-index form.delete").submit();
			}
		}
	});

	$("#foodcollocation-index tbody tr").hover(function(){
		$(this).css("background-color", "#d9e6ff");
	}, function(){
		$(this).css("background-color", "white");
	});

/*
	var text = $("#foodcollocation-add-edit .pic");
	var button = $("#foodcollocation-add-edit form .upload");
	var fileType = "pic",fileNum = "one";

	new AjaxUpload(button,{
		action: APP+'/FoodArticle/uploads',
		name: 'pic',
		onSubmit : function(file, ext){
			if(fileType == "image")
			{
				if (ext && /^(jpg|png|jpeg|gif)$/.test(ext)){
					this.setData({
						'info': '文件类型为图片'
					});
				} else {
					alert('非图片类型文件，请重传');
					return false;
				}
			}
		},
		onComplete: function(file, response){
			if(isNaN(response[0])){
				alert(response);

			}else{
				text.val(response);
				$("#foodcollocation-add-edit .img img").attr('src', PUBLIC+"/Content/tmp/"+response);
			}
		}
	});
*/

});
